package tableprocessor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author gleb
 */
public class CathegoryStaticTree
{
   private   DefaultMutableTreeNode root = new DefaultMutableTreeNode("Категории");
   private  List<Pare> clist = new ArrayList<Pare>();
   private  List<ColumnPare> colList = new ArrayList<ColumnPare>();
   public  boolean isHasColumns(DefaultMutableTreeNode node)
    {
        for (int i = 0 ; i < colList.size(); i++)
        {
            if (colList.get(i).getPare().getNode().equals(node))
            {
                return true;
            }
        }
        return false;
    }
   public  Iterator<ColumnPare> getColumnsForCathegory(DefaultMutableTreeNode node)
   {
       List<ColumnPare> ctmpList = new ArrayList<ColumnPare>();
       for (int i = 0 ; i < colList.size(); i++)
       {
           if (colList.get(i).getPare().getNode().equals(node))
           {
               ctmpList.add(colList.get(i));
           }
       }
       return ctmpList.iterator();
   }
    public  Pare findPareByNode(DefaultMutableTreeNode node)
    {
        if (root.equals(node))
        {
            return new Pare(null,root);
        }
        for (int i = 0; i < clist.size(); i++)
        {
            if (clist.get(i).getNode().equals(node))
            {
                return clist.get(i);
            }
        }
        return null;
    }
    private  Pare getForPareRoot(Pare p)
    {
        for (int i = 0 ; i < clist.size(); i++)
        {
            if (clist.get(i).getDbcat().getId() == p.getDbcat().getParent())
            {
                return clist.get(i);
            }
        }
        return null;
    }
    public  boolean isColumn(DefaultMutableTreeNode node)
    {
        for(int i = 0 ; i < colList.size(); i++)
        {
            if (colList.get(i).getNode().equals(node))
            {
                return true;
            }
        }
        return false;
    }
    public  boolean isHasChild(DefaultMutableTreeNode node)
    {
        Pare p = findPareByNode(node);
        if (p!=null)
        {
            return isHasChild(p.getDbcat().getId());
        }
        else
        {
            return false;
        }

    }
    public  boolean isHasChild(int id)
    {
        for (int i = 0 ; i < clist.size(); i++)
        {
            if (clist.get(i).getDbcat().getParent() == id)
            {
                return true;
            }
        }
        return false;
    }

    public  void fill(List<Pare> _clist,List<ColumnPare> _colList,DefaultMutableTreeNode _root )
    {
        clist = _clist;
        colList = _colList;
    }
    public DBColumn findColumnByNode(DefaultMutableTreeNode node)
    {
        for (int i = 0 ; i < colList.size(); i++)
        {
            if (colList.get(i).getNode().equals(node))
            {
                return colList.get(i).getDbcol();
            }
        }
        return null;
    }
}
